package com.wdciti.sccp.iot.tsdata.impl.iotdb;

import com.wdciti.sccp.iot.tsdata.core.api.TSDBQueryWrapper;
import com.wdciti.sccp.iot.tsdata.core.api.TSDBWrapper;
import com.wdciti.sccp.iot.tsdata.core.api.TsDataException;
import org.apache.iotdb.isession.pool.SessionDataSetWrapper;
import org.apache.iotdb.rpc.IoTDBConnectionException;
import org.apache.iotdb.rpc.StatementExecutionException;
import org.apache.iotdb.session.pool.SessionPool;


public class IotdbQueryWrapper implements TSDBQueryWrapper<SessionDataSetWrapper> {

    private final TSDBWrapper<SessionPool> sessionWrapper;
    private SessionDataSetWrapper dataSetWrapper;
    private String queryString;
    private TsDataException exception;

    public IotdbQueryWrapper(TSDBWrapper<SessionPool> sessionWrapper) {
        this.sessionWrapper = sessionWrapper;
    }

    @Override
    public void executeNonQuery(String sql) {
        try {
            this.queryString = sql;
            sessionWrapper.getSessionWrapper().executeNonQueryStatement(queryString);
        } catch (StatementExecutionException | IoTDBConnectionException e) {
            this.exception = new TsDataException(e);
        }
    }

    @Override
    public void executeQuery(String sql, long timeoutInMs) {
        try {
            this.queryString = sql;
            this.dataSetWrapper = sessionWrapper.getSessionWrapper().executeQueryStatement(queryString, timeoutInMs);
        } catch (IoTDBConnectionException | StatementExecutionException e) {
            this.exception = new TsDataException(e);
        }
    }

    @Override
    public TsDataException getException() {
        return this.exception;
    }

    @Override
    public SessionDataSetWrapper getResultData() {
        return this.dataSetWrapper;
    }


}
